
const MusicServire = require("../sercice/MusicServire")
const MusicControl = {
    add_music: async (req, res) => {
        console.log("??///")
        try {
            console.log("??///")
            const filePath = req.file.path
            // 将路径\\转化为/ 'public\\images\\user\\1716630232816-664794344.png',
            const normalizedPath = filePath.replace(/\\/g, '/');
            //去掉路径中的public
            const relativePath = normalizedPath.split('public')[1];
            res.status(200).send({
                msg: "上传成功",
                data: {
                    url: relativePath
                },
                code: 200
            })
        } catch (error) {
            res.status(411).send({
                msg: "上传失败",
                data: "",
                code: 411
            })
        }
    },
    add_music_document: async (req, res) => {
        try {
            const result = await MusicServire.add_music(req.body)
            console.log(result, '结果')
            res.status(200).send({
                msg: "发布成功",
                code: 200
            })
        } catch (error) {
            console.log(error)
            res.status(411).send({
                msg: "发布失败",
                code: 411
            })
        }
    },
    get_list: async (req, res) => {

        try {
            const result = await MusicServire.get_list(req.params.id)
            res.status(200).send(
                {
                    msg: "获取成功",
                    data: result.data,
                    count: result.count,
                    code: 200
                }
            )

        } catch (error) {
            res.status(411).send(
                {
                    msg: "获取失败",
                    data: '获取失败',
                    code: 411
                }
            )

        }
    },
    put_music: async (req, res) => {
        const result = await MusicServire.music_put(req.body)
        console.log(req.body)

        try {
            if (result.matchedCount === 1) {
                res.status(200).send({
                    msg: "修改成功",
                    code: 200
                })
            } else {
                res.status(411).send({
                    msg: "修改失败",
                    code: 411
                })
            }

        } catch (error) {
            res.status(411).send({
                msg: "修改失败",
                code: 411
            })
        }
    },
    delete_music: async (req, res) => {
        try {
            const result = await MusicServire.music_delete(req.params.id)
            console.log(result)
            res.status(200).send({
                msg: "删除成功",
                code: 200
            })
        } catch (error) {
            res.status(411).send({
                msg: "删除失败",
                code: 411
            })
        }

    }
}


module.exports = MusicControl